Pn = {
	version : '1.0'
};
/**
 * get url parameter
 */
Pn.getParam = function(key) {
	var params = location.search.substr(1).split('&');
	var kv;
	for ( var i = 0; i < params.length; i++) {
		kv = params[i].split('=');
		if (kv[0] == key) {
			return kv[1];
		}
	}
};

Pn.checkbox = function(name, checked) {
	if(checked){
		$("input[type='checkbox'][name="+name+"]").each(function(){
			$(this).next().addClass("checkboxChecked");
			$(this).attr("checked","checked");
		});
	}else{
		$("input[type='checkbox'][name="+name+"]").each(function(){
			$(this).next().removeClass("checkboxChecked");
			$(this).removeAttr("checked");
		});
	}
}

/**
 * 复选框选中的个数
 * 
 * @param name
 *            string of checkbox name
 */
Pn.checkedCount = function(name) {
	var count=0;
	$("input[type=checkbox][name='"+name+"']").each(function(){
		if($(this).attr('checked') || $(this).attr('checked')=='true' || $(this).attr('checked')=='checked'){
			count++;
		}
	});
	return count;
}
/**
 * 复选框选中ids
 * 
 * @param name string of checkbox name
 */
Pn.getCheckedIds = function(name){
	var array = new Array();
	$("input[type=checkbox][name='"+name+"']").each(function(){
		if($(this).attr('checked') || $(this).attr('checked')=='true' || $(this).attr('checked')=='checked'){
			array.push($(this).val());
		}
	});
	return array;
}


/**
 * 通过点击td 标签选中checkbox
 */
Pn.lineSelect = function(obj) {
	var checkLabel = $(obj).parent().find('.checkbox');

	$(checkLabel).click();
	var checked = checkLabel.hasClass('checkboxChecked');
	if(checked){
		var tableCheckBoxes = $('#pn-ltable .checkbox').length-1;//去掉全选复选框
		var hasCheckedBoxes = $('#pn-ltable .checkboxChecked').length;
		if(hasCheckedBoxes == tableCheckBoxes){
			$('#allCheck').next().addClass('checkboxChecked');
			$('#allCheck').attr('checked',checked);
		}
	}else{
		if($('#allCheck').next().hasClass('checkboxChecked')){
			$('#allCheck').next().removeClass('checkboxChecked');
			$('#allCheck').removeAttr('checked');	
		}
	}
};

Pn.Show = function(id) {
	$('#' + id).show();
}

Pn.Hide = function(id) {
	$('#' + id).hide();
}

Pn.ShowHide = function(id) {
	if($('.cmsmodel').length > 0){
		$('.cmsmodel').hide();		
	}
	var sh = $('#'+id);
	if (sh.css('display') == 'none') {
		sh.show();
	} else {
		sh.hide();
	}
	setTimeout("$('#"+id+"').hide()",5000);
}

/**
 * 颜色选择器 需要jquery.icolor.js支持
 */
Pn.ColorPicker = function(input) {
	var icolorId = $(input).attr('id')
	$(input).icolor(
			{
				colors : [ "ff0000", "000000", "0000ff", "003300", "003366",
						"000080", "333399", "333333", "800000", "ff6600",
						"808000", "008000", "008080", "333300", "666699",
						"808080", "993300", "ff9900", "99cc00", "339966",
						"33cccc", "3366ff", "800080", "999999", "ff00ff",
						"ffcc00", "ffff00", "00ff00", "00ffff", "00ccff",
						"993366", "cccccc", "ff99cc", "ffcc99", "ffff99",
						"ccffcc", "ccffff", "99ccff", "cc99ff", "ffffff" ],
				valueTargetId : $('#' + icolorId + "-value"),
				viewTargetId : $('#' + icolorId + "-view")
			});
}

$.fn.extend( {
	colorPicker : function() {
		new Pn.ColorPicker(this);
	}
});

/**
 * 文本加粗
 */
Pn.TxtBold = function(viewId, valueId) {
	var view = $("#" + viewId);
	var value = $("#" + valueId);
	var fontWeight = view.css("font-weight");
	if (fontWeight == 400) {
		view.css( {
			'font-weight' : 'bold'
		});
		value.val(true);
	} else {
		view.css( {
			'font-weight' : '400'
		});
		value.val(false);
	}
}

Pn.addBold = function(viewId,valueId){
	var view = JRF.dom.find("#" + viewId);
	var value = JRF.dom.find("#" + valueId);
	var fontWeight = view.css("font-weight");
	if (fontWeight == 400) {
		view.css( {
			'font-weight' : 'bold'
		});
		value.val(true);
	} else {
		view.css( {
			'font-weight' : '400'
		});
		value.val(false);
	}
}
/**
 * left页面滚动条
 * 使用于div 
 */
Pn.LeftPageInit = function(div){
	var leftTopHeight = $('#leftTopDiv').height();
	var h = $(window).height() - leftTopHeight;
	$(div).height(h);
	//自定义滚动条样式
	$(div).mCustomScrollbar({
		scrollButtons:{
			enable:true
		}
	});
}

/**
*right页面滚动条
*适用于div
*/
Pn.RightPageInit = function(div){
	var rightTopHeight = $(div).prev().height();
	var h = $(window).height() - rightTopHeight;
	$(div).height(h); 
	
}

$.fn.extend( {
	leftPageInit : function() {
		new Pn.LeftPageInit(this);
	}
});

$.fn.extend( {
	rightPageInit : function() {
		new Pn.RightPageInit(this);
	}
});

/**
 * 功能：自定义单选框 radio 
 * @param container
 */
Pn.RadioInit = function(container){
	if(JRF.isNull(container)){
		container=$(body);	
	}
	$(container).find(':radio').hide();
	$(container).find(".radio").click(function(){
		if(!$(this).hasClass("radioChecked")){
			$(this).prev().attr('checked',true).siblings().removeAttr('checked');
			$(this).prev().click();
			$(this).addClass('radioChecked').siblings().removeClass('radioChecked');
			$(this).prev().attr('checked',true).siblings().removeAttr('checked');
		}
	});
	//样式二选中事件
	$(container).find(".acradio").click(function(){
		if(!$(this).hasClass("achecked")){
			$(this).prev().attr('checked',true).siblings().removeAttr('checked');
			$(this).prev().click();
			$(this).addClass('achecked').parent().siblings().find(".acradio").removeClass('achecked');
			$(this).prev().attr('checked',true).parent().siblings().find(":radio").removeAttr('checked');
		}
	});
};


/**
*功能：自定义单选按钮
*/
$.fn.extend({
	radioInit: function(){
		new Pn.RadioInit(this);
	}
});

/**
 * 功能：自定义checbox 复选框
 */
Pn.CheckboxInit = function(container){
	if(!container){
		container = $(body);	
	}
	$(container).find(':checkbox').hide();
	$(container).find('.checkbox').click(function(){
		var checkbox = $(this).prev();
		$(this).toggleClass('checkboxChecked');
		var checked = $(this).hasClass('checkboxChecked');
		checkbox.attr('checked',checked);
		checkbox.click();
		checkbox.attr('checked',checked);
		if($(this).parents('table').hasClass('pn-ltable')){
			$(this).parents('tr').toggleClass('tdonclick');	
		}
	});
}

$.fn.extend({
	checkboxInit: function(){
		Pn.CheckboxInit(this);	
	}
});

/**
 * 功能：页面初始调优
 */
$.fn.extend({
	formInit: function(){
		$(this).radioInit();
		$(this).checkboxInit();	
	}	
});

/**
*功能：对两个标签进行class切换
*/
Pn.resetClass = function(id1,id2,className1,className2){
	$('#'+id1).addClass(className1).removeClass(className2);
	$('#'+id2).addClass(className2).removeClass(className1);
}


Pn.divHideByMouseout = function(e,o){
	var x = e.clientX;
	var y = e.clientY;
	var dxlft = $(o).offset().left;
	var dytop = $(o).offset().top;
	var dxrgt = dxlft + o.offsetWidth;
	var dybtm = dytop + o.offsetHeight;
	if(x < dxlft || x > dxrgt || y < dytop || y > dybtm){
		$('.global_copy').removeClass('global_copy');
		$(o).hide('blind');
	}
}

Pn.messageHideByMouseout = function(e,o){
	var x = e.clientX;
	var y = e.clientY;
	var dxlft = $(o).offset().left;
	var dytop = $(o).offset().top;
	var dxrgt = dxlft + o.offsetWidth;
	var dybtm = dytop + o.offsetHeight;
	if(x < dxlft || x > dxrgt || y < dytop || y > dybtm){
		$('.worklist').removeClass('worklist');
		$(o).hide('blind');
	}
}